<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="dao.*,entity.*" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'doLogin.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    This is my JSP page. <br>
    <%
    	request.setCharacterEncoding("utf-8");
    	long u=Long.parseLong(request.getParameter("uname"));
    	String p=request.getParameter("pass");
    	String code=request.getParameter("code");
    	String yzm=(String)request.getSession().getAttribute("yzm");
    	if(code.equals(yzm)==false){
    	  out.print("<script>alert('验证码错误');history.back();</script>");
    	  return;
    	}
    	StudentDaoI sdao=new StudentDao();
    	Student stu=sdao.login(u, p);
    	
    	//if(u.startsWith("liu")&&p.equals("123456"))
    	if(stu!=null)//关键
    	{
    	//登录成功后，我把用户名和密码放到cookie去
    	 	Cookie c=new Cookie("uname",String.valueOf(u));
    	 	c.setMaxAge(3600*24*7);
    	 	//>c.setPath("/");
    	 	Cookie c2=new Cookie("upass",p);
    	 	c2.setMaxAge(3600*24*7);
    	 	response.addCookie(c);
    	 	response.addCookie(c2);
    	 	
    		session.setAttribute("USER_LOGIN", stu);//!!!
    		//response.sendRedirect("8-28/stus/students.jsp");
    		response.sendRedirect("studentServlet");
    		return;
    	}
    	out.print("<script>alert('用户名或者密码错误');history.back();</script>");
     %>
  </body>
</html>
